package edu.cuit.avatar;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.math.RoundingMode;

/**
 * BigInteger可以表示任意大小的十进制整数
 * BigDecimal可以表示任意精度的十进制数
 * @author <a href="mailto:1020zhaodan@163.com">Adan</a>
 * @version 1.0
 * @date 2025/7/9 10:23
 */
public class BigDataDemo {
    public static void main(String[] args) {
        long number = Long.MAX_VALUE;
        System.out.println(number);
        long result = number + 100;
        System.out.println(result);

        float pi1 = 3.14159265358979323846f;
        double pi2 = 3.14159265358979323846;

        System.out.println("float pi = " + pi1);
        System.out.println("double pi = " + pi2);

        System.out.println("5! = " + factorial(5));
        System.out.println("50! = " + factorial(50));

        System.out.println("==============================BigInteger===============================");

        BigInteger number1 = new BigInteger("9223372036854775807");
        BigInteger number2 = new BigInteger("100");
        System.out.println(number1.add(number2));
        System.out.println(number1.subtract(number2));
        System.out.println(number1.multiply(number2));
        System.out.println(number1.divide(number2));

        System.out.println("==============================BigDecimal===============================");
        BigDecimal pi3 = new BigDecimal("3.14159265358979323846");
        System.out.println("BigDecimal pi = " + pi3);

        BigDecimal num1 = new BigDecimal("10");
        BigDecimal num2 = new BigDecimal("3");
        System.out.println(num1.divide(num2, new MathContext(8, RoundingMode.HALF_UP))); //3.3333333....

    }

    /**
     * 计算number的阶乘
     * @param number 整数
     * @return 阶乘结果
     */
    public static BigInteger factorial(int number){
        BigInteger result =  BigInteger.ONE;
        for (int i = number; i >= 1; i--)
            result = result.multiply(new BigInteger(i + ""));
        return result;
    }
}
